import os
from typing import Any

from utils.data_generate import load_yaml


def value(el_express: str) -> Any:
    """
    read config value form yaml file
    :param el_express: use ${variable name} to get value
    :return: config value
    """
    if el_express is None:
        raise Exception("file is not None")
    if not el_express.startswith("${") or not el_express.endswith("}"):
        return Exception("el_express error")
    clean_variable = el_express.replace("${", "").replace("}", "")
    key_list: list = clean_variable.split(".")
    config = load_yaml(CONFIG_YAML_PATH)
    curr = None
    for key in key_list:
        if not curr:
            curr = config[key]
        else:
            curr = curr[key]
    return curr


CONFIG_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_YAML_PATH = CONFIG_DIR + "\\" + "config.yaml"
LOGGER_LEVEL = value("${logger.level}")
